﻿using System;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Formats.Biff.OfficeArt
{
    internal struct OfficeArtCOLORREF
    {
        private readonly byte _blue;
        private readonly ColorFlags _flags;
        private readonly byte _green;
        private readonly byte _red;

        public OfficeArtCOLORREF(int value)
        {
            _red = (byte) (value & 0xFF);
            _green = (byte) ((value >> 8) & 0xFF);
            _blue = (byte) ((value >> 16) & 0xFF);
            _flags = (ColorFlags) ((value >> 24) & 0xFF);
            Contract.Assert(ToInt() == value);
        }

        public OfficeArtCOLORREF(byte red, byte green, byte blue, ColorFlags flags)
        {
            _red = red;
            _green = green;
            _blue = blue;
            _flags = flags;
        }

        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3}", _red, _green, _blue, _flags);
        }

        public int ToInt()
        {
            return _red | _green << 8 | _blue << 16 | ((byte) _flags) << 24;
        }

        #region Nested type: ColorFlags

        [Flags]
        internal enum ColorFlags : byte
        {
            None = 0,
            fPaletteIndex = 0x1,
            fPaletteRGB = 0x2,
            fSystemRGB = 0x4,
            fSchemeIndex = 0x8,
            fSysIndex = 0x10,
        }

        #endregion

        public static OfficeArtCOLORREF SchemeColor(byte i)
        {
            return new OfficeArtCOLORREF((byte) i, 0, 0, ColorFlags.fSchemeIndex);
        }

        public static OfficeArtCOLORREF SysColor(byte i)
        {
            return new OfficeArtCOLORREF((byte)i, 0, 0, ColorFlags.fSysIndex);
        }
    }
}